package com.ruoyi.web.controller.Ticket;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.ruoyi.common.config.AliConfig;
import com.ruoyi.common.config.BlueTicketConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import secure.access.encrypt.XjbwDataAccess;

@RestController
@RequestMapping("/ticket")
public class BlueTicketController {
    /**
     * 获取短信验证码
     */
    @PostMapping("/gainVerificationCode")
    public AjaxResult getgainVerificationCode(@RequestBody String content){
        String url = "api/billingAll/v1/user/gainVerificationCode";
        System.out.println(content);
        Object json = toJson(getAccess(url, content, "gainVerificationCode"));
        return AjaxResult.success(json);
    }
    /**
     * 用户登录
     */
    @PostMapping("/validateLogon")
    public AjaxResult getvalidateLogon(@RequestBody String content){
        String url = "api/billingAll/v1/user/validateLogon";
        System.out.println(content);
        Object json = toJson(getAccess(url, content, "validateLogon"));
        return AjaxResult.success(json);
    }
    /**
     * 人脸识别二维码
     */
    @PostMapping("/queryFaceRecognitionCode")
    public AjaxResult getqueryFaceRecognitionCode(@RequestBody String content){
        String url = "api/billingAll/v1/user/queryFaceRecognitionCode";
        System.out.println(content);
        Object json = toJson(getAccess(url, content, "queryAsynResult"));
        return AjaxResult.success(json);
    }

    /**
     * 查询获取人脸识别结果
     */
    @PostMapping("/queryFaceRecognitionResult")
    public AjaxResult getqueryFaceRecognitionResult(@RequestBody String content){
        String url = "api/billingAll/v1/user/queryFaceRecognitionResult";
        System.out.println(content);
        Object json = toJson(getAccess(url, content, "queryFaceRecognitionResult"));
        return AjaxResult.success(json);
    }
    /**
     * 蓝票开具接口
     * @param content
     * @return
     */
    @PostMapping("/invoiceOpening")
    public AjaxResult access(@RequestBody String content){
        String url = "api/billingAll/v1/invoice/invoiceOpening";
        System.out.println(content);
        String access = getAccess(url, content, "invoiceOpening");
        Object json = toJson(access);
        String s =  getTaskId(access);
        if(s=="taskID 字段不存在。"){
            return AjaxResult.error("查询失败",json);
        }
        System.out.println(getAccess(url, content, "invoiceOpening"));
        return AjaxResult.success("查询成功",s);
    }

    /**
     * 异步结果接口
     * @param content
     * @return
     */
    @PostMapping("/queryAsynResult")
    public AjaxResult getQueryAsynResult(@RequestBody String content){
        String url = "api/billingAll/v1/asyn/queryAsynResult";
        System.out.println(content);
        Object json = toJson(getAccess(url, content, "queryAsynResult"));
        return AjaxResult.success("",json);
    }

    /**
     * 4.1.12.获取发票下载地址 可以获取全电发票PDF、OFD、XML等格式的下载地址及二维码地址
     * @param content
     * @return
     */
    @PostMapping("/queryInvoiceDownloadUrl")
    public AjaxResult getqueryInvoiceDownloadUrl(@RequestBody String content){
        String url = "api/billingAll/v1/invoice/queryInvoiceDownloadUrl";
        System.out.println(content);
        Object json = toJson(getAccess(url, content, "queryInvoiceDownloadUrl"));
        return AjaxResult.success(json);
    }

    /**
     * 税局全电版式文件推送。
     * @param content
     * @return
     */
    @PostMapping("/pushFormatLink")
    public AjaxResult pushFormatLink(@RequestBody String content){
        String url = "api/invoicePlat/allEleInvoice/v1/pushFormatLink";
        System.out.println(content);
        Object json = toJson(getAccess(url, content, "allEleInvoicePushFormatLink"));
        return AjaxResult.success(json);
    }

    /**
     * 发送请求公共方法
     * @param url 请求地址
     * @param content 请求报文
     * @param businessCode 业务代码
     * @return
     */
    public String getAccess(String url,String content,String businessCode){
        String reqMsgId = AliConfig.generateOrderNumber("");
        String s = XjbwDataAccess.dataAccess(BlueTicketConfig.pubUrl + url, BlueTicketConfig.companyPriKey,
                BlueTicketConfig.platFormPubKey, content, BlueTicketConfig.accessType,
                BlueTicketConfig.enterpriseCode, BlueTicketConfig.thirdPlatformCode
                , reqMsgId, businessCode, "", "1"
        );
        return  s;
    }

    /**
     * 判断是否有taskID字段
     * @param string
     * @return
     */
    public String getTaskId(String string) {
        JsonParser parser = new JsonParser();
        JsonObject jsonObject = parser.parse(string).getAsJsonObject();

        // 检查是否存在 message 和 content 对象，以及 taskID 字段
        if (jsonObject.has("message")) {
            JsonObject messageObject = jsonObject.getAsJsonObject("message");

            if (messageObject.has("content")) {
                JsonObject contentObject = messageObject.getAsJsonObject("content");

                if (contentObject.has("taskID")) {
                    String taskID = contentObject.get("taskID").getAsString();
                    System.out.println("taskID: " + taskID);
                    return taskID;
                }
            }
        }
        return "taskID 字段不存在。";
    }
    public Object toJson(String s){
        Gson gson = new Gson();
        Object json = gson.fromJson(s, Object.class);
        return json;
    }
}
